package com.springboot.prac.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Desc:
 * @Author: siyuanlong
 * @Date: 2022/2/22 22:19
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.139.131");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        // 2.创建一个Connection
        Connection connection = connectionFactory.newConnection();

        // 3.获取一个信道
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("cc", BuiltinExchangeType.DIRECT, true);

        // 4.声明一个队列
        channel.queueDeclare("xiao", true, false, false, null);
        channel.queueBind("xiao", "cc", "mag01");


        // 5.创建消费者
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String s = new String(delivery.getBody(), "UTF-8");
            System.out.println(s);
        };
        channel.basicConsume("xiao", true, deliverCallback, consumerTag -> {
        });

        // 5.关闭资源
        System.in.read();
    }
}
